package com.example.fairplay.newfpregistration.camera;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.fairplay.newfpregistration.R;
import com.example.fairplay.newfpregistration.ui.DistinguishFaceActivity;
import com.example.fairplay.newfpregistration.ui.FaceToManyActivity;
import com.example.mylibrary.MyApp;
import com.example.mylibrary.config.Config;
import com.example.mylibrary.util.Logger;


public class FragmentCamera extends Fragment {
	CameraSurfaceView surfaceView = null;
	private ImageButton shutterBtn;
	private ImageButton switchBtn;
	private ImageView faceImg;
	private TextView tv_switch_type;
	private Logger logger;
	float previewRate = -1f;
	private View view;

	private Context context;
	private ViewGroup.LayoutParams params;
	private int mode_count;
	private MyApp app;
	private FaceView faceView;
	private byte[] IDCardData = null;
	private byte[] byte1;
	private byte[] byte2;
	private Button btnBack;
	private FacePicInterface inter;
	private Activity mActivity;
	private Button btnChange;


	public byte[] getBytes() {
		return byte1;
	}

	public byte[] getByte2() {
		return byte2;
	}

	public void setByte2(byte[] byte2) {
		this.byte2 = byte2;
	}

	public void setBytes(byte[] byte1) {
		this.byte1 = byte1;
	}

	public FaceView getFaceView() {
		return faceView;
	}

	public void setFaceView(FaceView faceView) {
		this.faceView = faceView;
	}

	public byte[] getIDCardData() {
		return IDCardData;
	}

	public void setIDCardData(byte[] IDCardData) {
		this.IDCardData = IDCardData;
	}

	@Override
	public void onAttach(Activity activity) {
		inter = (FacePicInterface) activity;
		mActivity = activity;

		app = MyApp.getInstance();
		super.onAttach(activity);
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	                         Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_camera1, container, false);
		context = getActivity();
		logger = new Logger();
		logger.setTag(FragmentCamera.class.getSimpleName());
		initUI();
		initViewParams(1);
		shutterBtn.setOnClickListener(new BtnListeners());
		switchBtn.setOnClickListener(new BtnListeners());
		btnBack.setOnClickListener(new BtnListeners());

		btnChange.setOnClickListener(new BtnListeners());
		// shutter mode : auto, manual
		if (app.getPreferences().getInt(Config.KEY_SHUTTER_MODE,
				Config.MODE_AUTO) == Config.MODE_AUTO) {
			shutterBtn.setEnabled(false);
			shutterBtn.setVisibility(View.GONE);
			faceImg.setVisibility(View.GONE);
		} else {
			shutterBtn.setEnabled(true);
			shutterBtn.setVisibility(View.VISIBLE);
			faceImg.setVisibility(View.VISIBLE);
		}


		return view;
	}


	@Override
	public void onResume() {
		super.onResume();
		app.faceDetectRectShowCount = 0;
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		inter.fromPic(222, 0, null);
	}


	private void initUI() {
		surfaceView = (CameraSurfaceView) view
				.findViewById(R.id.camera_surfaceview);
		shutterBtn = (ImageButton) view.findViewById(R.id.btn_shutter);
		switchBtn = (ImageButton) view.findViewById(R.id.btn_switch);
		faceView = (FaceView) view.findViewById(R.id.face_view);
		faceImg = (ImageView) view.findViewById(R.id.face_img);
		tv_switch_type = (TextView) view.findViewById(R.id.tv_switch_type);
		btnBack = (Button) view.findViewById(R.id.btn_back);
		btnChange = (Button) view.findViewById(R.id.btn_change_photo);
		if (mActivity instanceof DistinguishFaceActivity) {
			btnBack.setVisibility(View.GONE);
		} else if (mActivity instanceof FaceToManyActivity) {
			btnBack.setVisibility(View.VISIBLE);
		}
		int shutterMode = app.getPreferences().getInt(Config.KEY_SHUTTER_MODE,
				Config.MODE_AUTO);
		if (shutterMode == Config.MODE_AUTO)
			tv_switch_type.setText("自动拍照");
		else
			tv_switch_type.setText("手动拍照");
	}

	private void initViewParams(int no) {
		params = surfaceView.getLayoutParams();
		params.width = Config.WIDTH;
		params.height = Config.HEIGHT;
//		surfaceView.changeCamera(no);
		surfaceView.setLayoutParams(params);
	}

	private class BtnListeners implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btn_change_photo:
//					Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
//					int cameraCount = Camera.getNumberOfCameras();//得到摄像头的个数
//					if (cameraCount == 2) {
//						if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
//							initViewParams(1);
//						} else {
//							initViewParams(2);
//						}
//					}
					break;
				case R.id.btn_shutter:
					takePicture();
					break;
				case R.id.btn_switch:
					switchCameraMode();
					break;
				case R.id.btn_back:
					inter.fromPic(222, 0, null);
					break;
				default:
					break;
			}
		}
	}

	/**
	 * 切换拍照模式
	 */
	private void switchCameraMode() {
		int currentShutterMode = app.getPreferences().getInt(
				Config.KEY_SHUTTER_MODE, Config.MODE_AUTO);
		app.getPreferences()
				.edit()
				.putInt(Config.KEY_SHUTTER_MODE,
						(currentShutterMode == Config.MODE_AUTO) ? Config.MODE_MANUAL
								: Config.MODE_AUTO).commit();
		if (app.getPreferences().getInt(Config.KEY_SHUTTER_MODE,
				Config.MODE_AUTO) == Config.MODE_AUTO) {
			shutterBtn.setEnabled(false);
			shutterBtn.setVisibility(View.GONE);
			faceImg.setVisibility(View.GONE);
			tv_switch_type.setText("自动拍照");
		} else {
			shutterBtn.setEnabled(true);
			shutterBtn.setVisibility(View.VISIBLE);
			faceImg.setVisibility(View.VISIBLE);
			tv_switch_type.setText("手动拍照");
		}
	}

	/**
	 * 拍照
	 */
	private void takePicture() {
		CameraInterface.getInstance(context).doTakePicture();
	}


}
